package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.SetmealAddException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.utils.AliOssUtilDelete;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author majun
 * @package sky-take-out
 * @date 2024/5/14 : 上午11:27
 */
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private AliOssUtilDelete aliOssUtilDelete;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();

        //数据拷贝
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //默认禁用状态
        setmeal.setStatus(StatusConstant.DISABLE);

        //添加套餐数据
        setmealMapper.save(setmeal);

        Long id = setmeal.getId();

        //获取套餐菜品信息，加入套餐菜品表单
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //判断套餐是否包含菜品
        if (setmealDishes == null || setmealDishes.isEmpty()) {
            throw new SetmealAddException("套餐需要包含至少一种菜品");
        }

        //遍历赋值
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);

        });

        log.info("setmealDishes：{}", setmealDishes);

        //添加套餐绑定菜品数据
        setmealDishMapper.save(setmealDishes);
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getFromById(String id) {

        List<SetmealDish> setmealDishes = setmealDishMapper.getFromById(id);

        Setmeal setmeal = setmealMapper.getFromById(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {

        //查询套餐数据
        for (Long id : ids) {

            Setmeal setmeal = setmealMapper.getFromById(String.valueOf(id));

            //判断套餐是否起售中，起售中套餐不能删除
            if (setmeal.getStatus() == StatusConstant.DISABLE) {
                throw new BaseException("删除套餐中含有已起用套餐，启用中套餐无法删除");
            }

        }

        for (Long id : ids) {

            Setmeal setmeal = setmealMapper.getFromById(String.valueOf(id));

            //删除套餐关联菜品数据
            setmealDishMapper.detele(id);

            //删除套餐数据
            setmealMapper.detele(id);

            //删除ailOss上套餐图片
            String url = setmeal.getImage();
            String substring = url.substring(url.lastIndexOf("/") + 1);
            aliOssUtilDelete.delete(substring);

        }

    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        setmealDishMapper.detele(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && !setmealDishes.isEmpty()) {

            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });

            setmealDishMapper.save(setmealDishes);

        }

    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(String status, String id) {

        //判断套餐关联菜品是否全部是起售状态
        List<SetmealDish> fromById = setmealDishMapper.getFromById(id);
        for (SetmealDish setmealDish : fromById) {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishMapper.getById(dishId);
            Integer status1 = dish.getStatus();
            if (status1 == StatusConstant.DISABLE){
                throw new BaseException("该套餐有停售菜品");
            }
        }

        //修改套餐数据
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(Integer.valueOf(status));
        setmeal.setId(Long.valueOf(id));

        setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
